define(['vue',
		'config',
		'common',
		'../model/upLoadImg',
],function(Vue,config,utils,upLoadImg){
	'use strict';
	
	var shopApplicationView = new Vue({
		el:'#app',
		data:{
			province:'',
			city:'',
			district:'',
			licenseNumber:'',
			onprogress:'',
			count:0,
			fileList: {
				businessLicense: '',
				legalPic1: '',
				legalPic2: '',
				holdPic: '',
				facadePhoto: '',
				proxyPic: ''
			},
			submitBtn: false
		},
		created:function(){
			utils.userToken();
		  	this.applyshops = utils.getData('applyshops');
		},
		methods:{
			//第一次上传
			selectPhoto:function(event){
				var id = event.currentTarget.id;
				var $target = $(event.currentTarget);
				var $targetPic = $(event.currentTarget).parents('.Again').find('.imgAgain');

				$targetPic.show();
				$target.parents('.report-file').hide();

				var pic = upLoadImg.showPic($targetPic,$target);
				// 选择完立即压缩
				this.getFiles(id);
			},
			//再次上传
			selectAgain:function(event){
				var $target = $(event.currentTarget);
				var $targetPic = $target.parent('.imgAgain');
				var pic = upLoadImg.showPic($targetPic,$target);
				$target.parents('.Again').find('.file-prew').addClass('remove');
				var id = $target.parents('.Again').find('.file-prew').attr("id");
				// 选择完立即压缩
				this.getFiles(id);
			},
			getFiles:function(ele){
				var files;
				var _this = this;
				var element = $('#'+ele);
				if(element.hasClass('remove')){
					files = element.parents('.Again').find('.inputAgain').get(0).files[0];
				}else{
					files = element.get(0).files[0];
				}
				
				if(!files){
					_this.fileList[ele] = '';
				}else{
					lrz(files,{width:640})
					.then(function (rst) {
			        _this.fileList[ele] = rst.base64;
			        _this.count += 1;
			        })
			        .catch(function (err) {
			         _this.fileList[ele] = '';
			        })
			        .always(function () {
			        });
				}

			},

			toBankAccount:function(){
				var _this = this;
				
				var getPic = function(element,isLoading){
					var pic;
					if(isLoading === "t"){
						pic = $(element).find('.pic').attr('src');
					}else{
						pic = $(element).find('.pic');
					}
					return pic;
				}

				var onprogress = function(evt){
					var loaded = evt.loaded;     //已经上传大小情况 
					var tot = evt.total;      	 //附件总大小 
					var per = Math.floor(100*loaded/tot);  //已经上传的百分比
				    $('.upLoadimg').css({'height':(100-per)+'%'});
				}


				if($.trim(_this.licenseNumber) === ""){
					utils.appAlert('企业营业执照号不能为空');
                    return;
				}

				if(_this.licenseNumber.length < 15){
					utils.appAlert('企业营业执照号不能少于15位');
                    return;
				}

				if($.trim(getPic('#businessLicensePic','t')) === ""){
					utils.appAlert('请上传营业执照照片');
                    return;
				}
				
				if($.trim(getPic('#legalPic1Pic','t')) === ""){
					utils.appAlert('请上传法人身份证正面照片');
                    return;
				}

				if($.trim(getPic('#legalPic2Pic','t')) === ""){
					utils.appAlert('请上传法人身份证反面照片');
                    return;
				}

				if($.trim(getPic('#holdPicPic','t')) === ""){
					utils.appAlert('请上传负责人手持身份证照片');
                    return;
				}

				if($.trim(getPic('#facadePhotoPic','t')) === ""){
					utils.appAlert('请上传店面门头照片');
                    return;
				}

				if(_this.count < 5){
					utils.appAlert('图片处理中，请稍后...');
                    return;
				}

				if (_this.submitBtn) {
					return;
				}

				_this.submitBtn = true;
				
				var formData = new FormData();
				$.each(_this.fileList,function(k,p){
					if(p){
						formData.append("imgStr",p);
					}
				})
				upLoadImg.ajax(formData,onprogress,function(res){
					var urls = res.urls;
					getPic('#businessLicensePic').attr('src',urls[0]);
					getPic('#legalPic1Pic').attr('src',urls[1]);
					getPic('#legalPic2Pic').attr('src',urls[2]);
					getPic('#holdPicPic').attr('src',urls[3]);
					getPic('#facadePhotoPic').attr('src',urls[4]);
					getPic('#proxyPicPic').attr('src',urls[5]);

					var applyshops = utils.getData('applyshops');
					var data = {
						shopsName: applyshops.shopsName,
						shopsAddress: applyshops.shopsAddress,
						shopsLinkman: applyshops.shopsLinkman,
						shopsLinkphone: applyshops.shopsLinkphone,
						licenseNumber: _this.licenseNumber,
						businessLicense: urls[0],
						legalPic1: urls[1],
						legalPic2: urls[2],
						holdPic: urls[3],
						facadePhoto: urls[4],
						proxyPic: urls[5],
						province: applyshops.provinceID,
						city: applyshops.cityID,
						county: applyshops.countyID,
						area: applyshops.areaID
					}
					utils.ajax(utils.getWebUrl('shop/apply'),data,'POST',function(res){
						window.location.href="shopApplication_verify.html";
					},null,null,null,function(){
						_this.submitBtn = false;
						utils.appAlert(res.msg);
					})
				},null,null,null,function(res){
					_this.submitBtn = false;
					utils.appAlert(res.msg);
				})

				

				// var filesList = {
				// 	businessLicense: getFiles('#businessLicense'),
				// 	legalPic1: getFiles('#legalPic1'),
				// 	legalPic2: getFiles('#legalPic2'),
				// 	holdPic: getFiles('#holdPic'),
				// 	facadePhoto: getFiles('#facadePhoto'),
				// 	proxyPic: getFiles('#proxyPic')
				// }

				// utils.appAlert('当前上传图片数量较大,请在wifi环境下进行操作',null,
				// 	'取消',function(){
				// 		var onprogress = function(evt){
				// 			var loaded = evt.loaded;     //已经上传大小情况 
				// 			var tot = evt.total;      	 //附件总大小 
				// 			var per = Math.floor(100*loaded/tot);  //已经上传的百分比
				// 		    $('.upLoadimg').css({'height':(100-per)+'%'});
				// 		}

				// 		var format = upLoadImg.uploadFile(filesList);
				// 		upLoadImg.ajax(format,onprogress,function(res){
				// 			var urls = res.urls;
				// 			getPic('#businessLicensePic').attr('src',urls[0]);
				// 			getPic('#legalPic1Pic').attr('src',urls[1]);
				// 			getPic('#legalPic2Pic').attr('src',urls[2]);
				// 			getPic('#holdPicPic').attr('src',urls[3]);
				// 			getPic('#facadePhotoPic').attr('src',urls[4]);
				// 			getPic('#proxyPicPic').attr('src',urls[5]);

				// 			var applyshops = utils.getData('applyshops');
				// 			var data = {
				// 				shopsName: applyshops.shopsName,
				// 				shopsAddress: applyshops.shopsAddress,
				// 				shopsLinkman: applyshops.shopsLinkman,
				// 				shopsLinkphone: applyshops.shopsLinkphone,
				// 				licenseNumber: _this.licenseNumber,
				// 				businessLicense: urls[0],
				// 				legalPic1: urls[1],
				// 				legalPic2: urls[2],
				// 				holdPic: urls[3],
				// 				facadePhoto: urls[4],
				// 				proxyPic: urls[5],
				// 				province: applyshops.provinceID,
				// 				city: applyshops.cityID,
				// 				county: applyshops.countyID,
				// 				area: applyshops.areaID
				// 			}

				// 			utils.ajax(utils.getWebUrl('shop/apply'),data,'POST',function(res){
				// 				window.location.href="shopApplication_verify.html";
				// 			})
				// 		})
				// 	},'继续操作','提示')

				
			}
		}
	})

	return shopApplicationView
});